/**
 * Author: w景洋.
 * Time: 2016/6/15 0015
 */
angular.module('mall.detail', [])
    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('detail', {
                    url: '/detail/:shopid/:goodsid',
                    templateUrl: templateprefix + 'detail/detail.html',
                    resolve: {
                        shopinfo: ['$stateParams', 'ShopService', function ($stateParams, ShopService) {
                            var shopinfo = ShopService.queryShopByShopid($stateParams.shopid);
                            if (shopinfo.$promise) {
                                return shopinfo.$promise;
                            }
                            return shopinfo;
                        }],
                        menulist: ['$stateParams', 'ShopMenuService', function ($stateParams, ShopMenuService) {
                            var menulist = ShopMenuService.getMenuByShopid($stateParams.shopid);
                            return menulist;
                        }]
                    },
                    controller: 'GoodsDetailController'
                })
        }
    ])

    //
    .controller('GoodsDetailController', ['$scope','$state', '$stateParams', '$window',  'ShopMenuService', 'GoodsService', 'shopinfo',
        function ($scope,$state, $stateParams, $window,  ShopMenuService, GoodsService, shopinfo) {
            $scope.goodsid = $stateParams.goodsid;
            $scope.mall_enturl=projectBaseUrl;
            GoodsService.getGoodsByGoodsId($scope.goodsid).$promise.then(function (data) {
                $scope.good = data;
                $scope.good.num = ShopMenuService.getBuycountByGoodsid(data.shopid, data.cid, data.goodsid);
            });
            $scope.shopid = $stateParams.shopid;
            $scope.shopname = shopinfo.name;
            $scope.add = function (cid, goodsid) {
                ShopMenuService.addGood($scope.shopid, cid, goodsid);
                $scope.good.num = ShopMenuService.getBuycountByGoodsid($scope.good.shopid, $scope.good.cid, $scope.good.goodsid);
            }
            $scope.remove = function (cid, goodsid) {
                ShopMenuService.removeGood($scope.shopid, cid, goodsid);
                $scope.good.num = ShopMenuService.getBuycountByGoodsid($scope.shopid,cid, goodsid);
            }
            $scope.buy_change = function(cid, goodsid,num){
                ShopMenuService.inputChange($scope.shopid,cid, goodsid,num);
                if($scope.good.num>999){
                    $scope.good.num = 999;
                }else if($scope.good.num<0){
                    $scope.good.num = 0;
                }
            }
            $scope.inputBlur = function(cid, goodsid,num){
                ShopMenuService.inputBlur($scope.shopid,cid, goodsid,num);
                if($scope.good.num==undefined){
                    $scope.good.num = 0;
                }
            }
            //加入购物车
            $scope.addToCart = function(){
                if($scope.good.num == 0){
                    input_alert("请添加商品数量");
                }else{
                    ShopMenuService.addToCartShopById($scope.shopid);
                    $("#demo").hide();
                    $("#hidval").val("0");
                    input_alert("加入购物车成功");
                }

            }
            //立即购买操作
            $scope.buyNow = function(){
                var shopList = ShopMenuService.buyNowShopById($scope.shopid);
                if($scope.good.num==0){
                    input_alert("请添加商品数量");
                }else{
                    $state.go("placeorder", {order: shopList});
                }
            }
        }
    ])
;
